
require 'raklur/rdebug'

module Raklur

	class AccountValidationError < RuntimeError ; end

	# Abstract class for Accounts
	#
	class Account
		attr_accessor :type, :title
		attr_accessor :username, :password
		attr_accessor :server, :port

		# delay between polling for new info
		attr_accessor :update_delay
		
		# minimal_delay => delay under which you'll be considered aggressive or banned
		

		#
		# Initialize account with +title+ string and +data+ hash.
		#
		def initialize title, data
			@title = title
			@type = data['type']
			@username = data['username']
			@password = data['password']
#			@server = data['server']
#			@port = data['port']
			@update_delay = data['update_delay']
			#@minimal_delay = data['minimal_delay']

			data.keys.each do |key|
				val = data[key]
				validate_config_arg key, val
			end
		end

		#
		# Export local configuration as a hash.
		#
		def export_config
			data = {}
			return data
		end

		# 
		# Validates input configuration tuples
		# Raises an exception on error.
		# 
		def validate_config_arg key, value
			case key
			when 'type' then
			when 'username' then
			when 'password' then
			when 'update_delay' then
			else
			raise AccountValidationError, "Unknown parameter '#{key}' for account #{@title}"
			end
		end


		# 
		# Final validation for coherence
		#
		def validate_config
			raise 'type not defined' unless not @type.nil?
			raise 'Username not defined' unless not @username.nil?
			raise 'Password not defined' unless not @password.nil?
#			raise 'Server not defined' unless not @server.nil?
#			raise 'Port not defined' unless not @port.nil?
			raise 'Delay between updates not defined' unless not @update_delay.nil?
			#raise 'Minimal delay between updates not defined' unless not @minimal_delay.nil?
		end

		def prepare!
			rdebug "enable accounts ?"
			enable
		end


		def to_s
			title_str = if @title.nil? then "(none)"
						   else @title
						   end
			type_str = if @type.nil? then "(none)"
					   else @type
					   end
			username_str = if @username.nil? then "(none)"
							  else @username
							  end
			password_str = if @password.nil? then "(none)"
						 else "%c%s" % [ @password[0], ( '*' * ( @password.length - 1 ) ) ]
						 end

			str = "[%s]\n" % title_str +
				"  type = %s\n" % type_str + 
				"  username = %s\n" % username_str +
				"  password = %s\n" % password_str

			str
		end


		def streams
			raise NotImplementedError
		end

		private

	end
end
